import 'dart:io';

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

final _net = RegExp("^http");
final _assest = RegExp("^static");
final _file = RegExp("^file");

class CommImage extends StatelessWidget {
  final String src;
  final double width;
  final double height;
  final BoxFit fit;

  const CommImage(this.src, {this.width, this.height, this.fit, Key key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (_assest.hasMatch(src)) {
      return Image.asset(
        src,
        width: width,
        height: height,
        fit: fit,
      );
    }
    if (_file.hasMatch(src)) {
      return Image.file(
        File(src),
        width: width,
        height: height,
        fit: fit,
      );
    }

    if (_net.hasMatch(src)) {
      return Image(
        image: CachedNetworkImageProvider(src),
        width: width,
        height: height,
        fit: fit,
      );
    }
    assert(false, "src 必须 是往图片");
    return Container();
  }
}
